/****************************************************************************
**
** Copyright (C) 2018 Alexander Akulich <akulichalexander@gmail.com>
**
** Sailfish Silica UI is under copyright of Jolla Ltd.
**
** Based on Qt Material style
**
****************************************************************************/

import QtQuick 2.9
import QtQuick.Controls.Silica 2.0
import QtQuick.Controls.Silica.impl 2.0

import QtQuick.Controls.Material 2.0 as M
import QtQuick.Controls.Material.impl 2.0 as M


Item {
    id: indicator
    implicitWidth: 38
    implicitHeight: 32

    property Item control
    property alias handle: handle

    Silica.elevation: 1

    Rectangle {
        width: parent.width
        height: 14
        radius: height / 2
        y: parent.height / 2 - height / 2
        color: control.enabled ? (control.checked ? control.Silica.switchCheckedTrackColor : control.Silica.switchUncheckedTrackColor)
                               : control.Silica.switchDisabledTrackColor
    }

    Rectangle {
        id: handle
        x: Math.max(0, Math.min(parent.width - width, control.visualPosition * parent.width - (width / 2)))
        y: (parent.height - height) / 2
        width: 20
        height: 20
        radius: width / 2
        color: control.enabled ? (control.checked ? control.Silica.switchCheckedHandleColor : control.Silica.switchUncheckedHandleColor)
                               : control.Silica.switchDisabledHandleColor

        Behavior on x {
            enabled: !control.pressed
            SmoothedAnimation {
                duration: 300
            }
        }
        layer.enabled: indicator.Silica.elevation > 0
        layer.effect: M.ElevationEffect {
            elevation: indicator.Silica.elevation
        }
    }
}
